package com.jifen.pay.controller;
/**
 * 类名：PayController
 * 作者：zhangtao
 * 日期：2019/7/2 11:21
 * 版本：1.0.0
 * 描述：TODO
 **/
import com.jifen.common.CommonResult;
import com.jifen.common.Result;
import com.jifen.pay.service.PayService;
import com.jifen.pay.service.WeixinService;
import com.jifen.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/jf/{version}/pay")
public class PayController {

    @Autowired
    PayService payService;
    @Autowired
    WeixinService weixinService;

    /**
     * 商家充值积分
     * @param payType
     * @param token
     * @param jifen
     * @param request
     * @return
     */
    @RequestMapping("/mch/recharge")
    @ResponseBody
    public Result mchRecharge(int payType, String token, int jifen, HttpServletRequest request){
        Object rst = payService.mchRecharge(payType, token, jifen, SystemUtils.getRemoteAddr(request));
        return CommonResult.createSuccessResult(rst);
    }

    /**
     * 充值微信回调
     * @param request
     * @return
     */
    @RequestMapping("/mch/recharge/weixin/callback")
    public void rechargeWeixinCallBack(HttpServletRequest request, HttpServletResponse response){
        try {
            String rstXml = weixinService.mchRechargeCallback(request);
            if(rstXml!=null){
                response.getWriter().write(rstXml);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 超市交易
     * @param payType
     * @param token
     * @param content
     * @param request
     * @return
     */
    @RequestMapping("/smarket/trade")
    @ResponseBody
    public Result smarketTrade(int payType, String token, String content, HttpServletRequest request){
        Object rst = payService.smarketTrade(payType, token, content, SystemUtils.getRemoteAddr(request));
        return CommonResult.createSuccessResult(rst);
    }

    /**
     * 超市交易回调
     * @param request
     * @param response
     */
    @RequestMapping("/smarket/trade/weixin/callback")
    public void smarketTradeWeixinCallBack(HttpServletRequest request, HttpServletResponse response){
        try {
            String rstXml = weixinService.smarketTradeCallback(request);
            if(rstXml!=null){
                response.getWriter().write(rstXml);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
